﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlClient;
using System.Data;
using System.Collections;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;

using DataAccess;
using Entities;
using Common;

namespace BusinessLogic
{
    public static class KhachHang
    {
        // Selct khách hàng
        public static Entities.KhachHang[] Select(Entities.KhachHang nv)
        {
            Entities.KhachHang[] list = null;
            try
            {
                //lay mo ket noi voi' chuoi~ ket noi lay' tu` file app.confg trong GUI
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhachHang.SelectKhachHang, cn);
                cmd.Parameters.Add(Common.Constants.KhachHang.HanhDong, SqlDbType.NVarChar).Value = nv.HanhDong;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\CustomerImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.KhachHang khachhang = new Entities.KhachHang();
                    khachhang.KhachHangID = int.Parse(dr[Common.Constants.KhachHang.KhachHangID].ToString());
                    khachhang.Ten = dr[Common.Constants.KhachHang.Ten].ToString();
                    khachhang.GioiTinh = bool.Parse(dr[Common.Constants.KhachHang.GioiTinh].ToString());
                    khachhang.DiaChi = dr[Common.Constants.KhachHang.DiaChi].ToString();
                    khachhang.DienThoaiCD = dr[Common.Constants.KhachHang.DienThoaiCD].ToString();
                    khachhang.DienThoaiDD = dr[Common.Constants.KhachHang.DienThoaiDD].ToString();
                    khachhang.Fax = dr[Common.Constants.KhachHang.Fax].ToString();
                    khachhang.Email = dr[Common.Constants.KhachHang.Email].ToString();
                    khachhang.MST = dr[Common.Constants.KhachHang.MST].ToString();
                    khachhang.CongTy = dr[Common.Constants.KhachHang.CongTy].ToString();
                    khachhang.NgaySinh = DateTime.Parse(dr[Common.Constants.KhachHang.NgaySinh].ToString());
                    khachhang.MaVung = int.Parse(dr[Common.Constants.KhachHang.MaVung].ToString());
                    khachhang.NgayThamGia = DateTime.Parse(dr[Common.Constants.KhachHang.NgayThamGia].ToString());
                    khachhang.GiaoDichCuoi = DateTime.Parse(dr[Common.Constants.KhachHang.GiaoDichCuoi].ToString());
                    khachhang.Website = dr[Common.Constants.KhachHang.Website].ToString();
                    khachhang.DuNo = dr[Common.Constants.KhachHang.DuNo].ToString();
                    khachhang.HanMucThanhToan = dr[Common.Constants.KhachHang.HanMucThanhToan].ToString();
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\CustomerImage\" + dr[Common.Constants.KhachHang.LinkImage].ToString();
                    khachhang.LinkImage = dr[Common.Constants.KhachHang.LinkImage].ToString();
                    khachhang.LinkImageRoot = links;
                    khachhang.Image = null;
                    khachhang.GhiChu = dr[Common.Constants.KhachHang.GhiChu].ToString();
                    khachhang.Deleted = bool.Parse(dr[Common.Constants.KhachHang.Deleted].ToString());
                    khachhang.MaKhachHang = dr[Common.Constants.KhachHang.MaKhachHang].ToString();
                    arr.Add(khachhang);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = null;
                }
                else
                {
                    list = new Entities.KhachHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhachHang)arr[i];
                    }
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();
            }
            catch (Exception ex)
            {
                string s = ex.Message.ToString();
                list = null;
            }
            return list;

        }
        // xử lý ảnh
        private static byte[] XuLyAnh(string links)
        {

            byte[] image = null;
            try
            {
                Bitmap bmp = new Bitmap(links);
                System.IO.MemoryStream ms = new System.IO.MemoryStream();
                bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] manganh = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(manganh, 0, Convert.ToInt32(ms.Length));
                image = manganh;

                ms.Dispose();
                ms.Close();
                ms.Flush();
                bmp.Dispose();
                bmp.Clone();
            }
            catch (Exception ex)
            { string s = ex.Message; }
            return image;
        }
        // insert update
        public static int InsertUpdate(Entities.KhachHang khachhang)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                string sql = Common.Constants.KhachHang.InsertUpdateKhachHang;
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.KhachHang.HanhDong, SqlDbType.NVarChar, 50).Value = khachhang.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhachHang.KhachHangID, SqlDbType.Int).Value = khachhang.KhachHangID;
                cmd.Parameters.Add(Common.Constants.KhachHang.MaKhachHang, SqlDbType.NVarChar, 50).Value = khachhang.MaKhachHang;
                cmd.Parameters.Add(Common.Constants.KhachHang.Ten, SqlDbType.NVarChar, 200).Value = khachhang.Ten;
                cmd.Parameters.Add(Common.Constants.KhachHang.GioiTinh, SqlDbType.Bit).Value = khachhang.GioiTinh;
                cmd.Parameters.Add(Common.Constants.KhachHang.DiaChi, SqlDbType.NVarChar, 200).Value = khachhang.DiaChi;
                cmd.Parameters.Add(Common.Constants.KhachHang.DienThoaiCD, SqlDbType.NVarChar, 20).Value = khachhang.DienThoaiCD;
                cmd.Parameters.Add(Common.Constants.KhachHang.DienThoaiDD, SqlDbType.NVarChar, 20).Value = khachhang.DienThoaiDD;
                cmd.Parameters.Add(Common.Constants.KhachHang.Fax, SqlDbType.NVarChar, 20).Value = khachhang.Fax;
                cmd.Parameters.Add(Common.Constants.KhachHang.Email, SqlDbType.NVarChar, 50).Value = khachhang.Email;
                cmd.Parameters.Add(Common.Constants.KhachHang.MST, SqlDbType.NVarChar, 20).Value = khachhang.MST;
                cmd.Parameters.Add(Common.Constants.KhachHang.CongTy, SqlDbType.NVarChar, 200).Value = khachhang.CongTy;
                cmd.Parameters.Add(Common.Constants.KhachHang.NgaySinh, SqlDbType.DateTime).Value = khachhang.NgaySinh;
                cmd.Parameters.Add(Common.Constants.KhachHang.MaVung, SqlDbType.Int).Value = khachhang.MaVung;
                cmd.Parameters.Add(Common.Constants.KhachHang.NgayThamGia, SqlDbType.DateTime).Value = khachhang.NgayThamGia;
                cmd.Parameters.Add(Common.Constants.KhachHang.GiaoDichCuoi, SqlDbType.DateTime).Value = khachhang.GiaoDichCuoi;
                cmd.Parameters.Add(Common.Constants.KhachHang.Website, SqlDbType.VarChar, 200).Value = khachhang.Website;
                cmd.Parameters.Add(Common.Constants.KhachHang.DuNo, SqlDbType.Float).Value = khachhang.DuNo;
                cmd.Parameters.Add(Common.Constants.KhachHang.HanMucThanhToan, SqlDbType.Float).Value = khachhang.HanMucThanhToan;
                string linked = string.Empty;
                if (khachhang.LinkImage != string.Empty)
                {
                    linked = khachhang.MaKhachHang + ".jpeg";
                }
                else
                {
                    linked = string.Empty;
                }
                cmd.Parameters.Add(Common.Constants.KhachHang.LinkImage, SqlDbType.NVarChar).Value = linked;
                cmd.Parameters.Add(Common.Constants.KhachHang.GhiChu, SqlDbType.NVarChar, 200).Value = khachhang.GhiChu;
                cmd.Parameters.Add(Common.Constants.KhachHang.Deleted, SqlDbType.Bit).Value = khachhang.Deleted;

                rt = cmd.ExecuteNonQuery();

                if (rt != 0)
                {
                    if (linked != string.Empty)
                    {
                        string links = @"\CustomerImage\" + linked;
                        string linksRoot = System.Windows.Forms.Application.StartupPath.ToString() + links;
                        try { Common.Utilities.DeleteFile(links); }
                        catch { }
                        System.IO.MemoryStream ms = new System.IO.MemoryStream(khachhang.Image);
                        Bitmap bmp = new Bitmap(ms);
                        try
                        {
                            bmp.Save(linksRoot, System.Drawing.Imaging.ImageFormat.Jpeg);
                        }
                        catch { }
                    }
                }

            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }

        public static byte[] GetImage(string linksRoot)
        {
            byte[] image = null;
            try
            {
                image = Common.Utilities.GetImage(linksRoot);
            }
            catch (Exception ex)
            { string s = ex.Message; image = null; }
            return image;
        }
        // insert update
        public static int Delete(Entities.KhachHang khachhang)
        {
            int rt = 0;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                string sql = Common.Constants.KhachHang.DeleteKhachHang;
                SqlCommand cmd = new SqlCommand(sql, cn);
                cmd.Parameters.Add(Common.Constants.KhachHang.HanhDong, SqlDbType.VarChar).Value = khachhang.HanhDong;
                cmd.Parameters.Add(Common.Constants.KhachHang.MaKhachHang, SqlDbType.VarChar).Value = khachhang.MaKhachHang;
                rt = cmd.ExecuteNonQuery();
                if (rt != 0)
                {
                    //truyen vao ten file anh
                    Common.Utilities.DeleteFile(khachhang.LinkImage);
                }
                //xoa bo nho dong ket noi
                cmd.Connection.Dispose();
                cn.Close();
                DataAccess.Connection.CloseConnection();

            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return 0; }
            return rt;
        }



        /// <summary>
        ///  loivt :  Selct khách hàng
        /// </summary>
        /// <returns></returns>
        public static Entities.KhachHang[] Select()
        {
            Entities.KhachHang[] list = null;
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhachHang.SelectKH, cn);
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                ArrayList arr = new ArrayList();
                string directory = System.Windows.Forms.Application.StartupPath.ToString() + @"\CustomerImage";
                Common.Utilities.CreateDirectory(directory);
                while (dr.Read())
                {
                    Entities.KhachHang khachhang = new Entities.KhachHang();
                    khachhang.KhachHangID = int.Parse(dr[Common.Constants.KhachHang.KhachHangID].ToString());
                    khachhang.MaKhachHang = dr[Common.Constants.KhachHang.MaKhachHang].ToString();
                    khachhang.Ten = dr[Common.Constants.KhachHang.Ten].ToString();
                    khachhang.GioiTinh = bool.Parse(dr[Common.Constants.KhachHang.GioiTinh].ToString());
                    khachhang.DiaChi = dr[Common.Constants.KhachHang.DiaChi].ToString();
                    khachhang.DienThoaiCD = dr[Common.Constants.KhachHang.DienThoaiCD].ToString();
                    khachhang.DienThoaiDD = dr[Common.Constants.KhachHang.DienThoaiDD].ToString();
                    khachhang.Fax = dr[Common.Constants.KhachHang.Fax].ToString();
                    khachhang.Email = dr[Common.Constants.KhachHang.Email].ToString();
                    khachhang.MST = dr[Common.Constants.KhachHang.MST].ToString();
                    khachhang.CongTy = dr[Common.Constants.KhachHang.CongTy].ToString();
                    khachhang.NgaySinh = DateTime.Parse(dr[Common.Constants.KhachHang.NgaySinh].ToString());
                    khachhang.MaVung = int.Parse(dr[Common.Constants.KhachHang.MaVung].ToString());
                    khachhang.NgayThamGia = DateTime.Parse(dr[Common.Constants.KhachHang.NgayThamGia].ToString());
                    khachhang.GiaoDichCuoi = DateTime.Parse(dr[Common.Constants.KhachHang.GiaoDichCuoi].ToString());
                    khachhang.Website = dr[Common.Constants.KhachHang.Website].ToString();
                    khachhang.DuNo = dr[Common.Constants.KhachHang.DuNo].ToString();
                    khachhang.HanMucThanhToan = dr[Common.Constants.KhachHang.HanMucThanhToan].ToString();
                    khachhang.GhiChu = dr[Common.Constants.KhachHang.GhiChu].ToString();
                    khachhang.Deleted = bool.Parse(dr[Common.Constants.KhachHang.Deleted].ToString());
                    arr.Add(khachhang);
                }
                int n = arr.Count;
                if (n == 0)
                {
                    list = new Entities.KhachHang[0];
                }
                else
                {
                    list = new Entities.KhachHang[n];
                    for (int i = 0; i < n; i++)
                    {
                        list[i] = (Entities.KhachHang)arr[i];
                    }
                }
                cmd.Connection.Dispose();
                cn.Close();
                return list;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.KhachHang[0]; }


        }

        // Selct khách hàng
        public static Entities.KhachHang SelectTheoMa(Entities.KhachHang khachhang1)
        {
            Entities.KhachHang khachhang = new Entities.KhachHang();
            try
            {
                SqlConnection cn = DataAccess.Connection.OpenConnection();
                SqlCommand cmd = new SqlCommand(Common.Constants.KhachHang.SelectTheoMaKhachHang, cn);
                cmd.Parameters.Add(Common.Constants.KhachHang.MaKhachHang, SqlDbType.NVarChar, 50).Value = khachhang1.MaKhachHang;
                SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                while (dr.Read())
                {
                    khachhang = new Entities.KhachHang();
                    khachhang.KhachHangID = int.Parse(dr[Common.Constants.KhachHang.KhachHangID].ToString());
                    khachhang.MaKhachHang = dr[Common.Constants.KhachHang.MaKhachHang].ToString();
                    khachhang.Ten = dr[Common.Constants.KhachHang.Ten].ToString();
                    khachhang.GioiTinh = bool.Parse(dr[Common.Constants.KhachHang.GioiTinh].ToString());
                    khachhang.DiaChi = dr[Common.Constants.KhachHang.DiaChi].ToString();
                    khachhang.DienThoaiCD = dr[Common.Constants.KhachHang.DienThoaiCD].ToString();
                    khachhang.DienThoaiDD = dr[Common.Constants.KhachHang.DienThoaiDD].ToString();
                    khachhang.Fax = dr[Common.Constants.KhachHang.Fax].ToString();
                    khachhang.Email = dr[Common.Constants.KhachHang.Email].ToString();
                    khachhang.MST = dr[Common.Constants.KhachHang.MST].ToString();
                    khachhang.CongTy = dr[Common.Constants.KhachHang.CongTy].ToString();
                    khachhang.NgaySinh = DateTime.Parse(dr[Common.Constants.KhachHang.NgaySinh].ToString());
                    khachhang.MaVung = int.Parse(dr[Common.Constants.KhachHang.MaVung].ToString());
                    khachhang.NgayThamGia = DateTime.Parse(dr[Common.Constants.KhachHang.NgayThamGia].ToString());
                    khachhang.GiaoDichCuoi = DateTime.Parse(dr[Common.Constants.KhachHang.GiaoDichCuoi].ToString());
                    khachhang.Website = dr[Common.Constants.KhachHang.Website].ToString();
                    khachhang.DuNo = dr[Common.Constants.KhachHang.DuNo].ToString();
                    khachhang.HanMucThanhToan = dr[Common.Constants.KhachHang.HanMucThanhToan].ToString();
                    khachhang.GhiChu = dr[Common.Constants.KhachHang.GhiChu].ToString();
                    khachhang.Deleted = bool.Parse(dr[Common.Constants.KhachHang.Deleted].ToString());
                    string links = System.Windows.Forms.Application.StartupPath.ToString() + @"\CustomerImage\" + dr[Common.Constants.KhachHang.LinkImage].ToString();
                    khachhang.LinkImage = dr[Common.Constants.KhachHang.LinkImage].ToString();
                    khachhang.LinkImageRoot = links;
                    if (dr[Common.Constants.KhachHang.LinkImage].ToString() != "")
                    {
                        khachhang.LinkImage = System.Windows.Forms.Application.StartupPath.ToString() + @"\CustomerImage\" + dr[Common.Constants.KhachHang.LinkImage].ToString() + ".jpg";
                        try
                        {
                            khachhang.Image = XuLyAnh(khachhang.LinkImage);
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        khachhang.LinkImage = null;
                        khachhang.Image = null;
                    }
                }

                return khachhang;
            }
            catch (Exception ex)
            { string s = ex.Message.ToString(); return new Entities.KhachHang(); }
            finally
            {
            }

        }
    }

}
